<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>司机管理</el-breadcrumb-item>
      <el-breadcrumb-item>司机列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card style="background-color: #100c2a;margin-top: 15px;border:2px solid #4f7f9c">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @click="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList">搜索司机</el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAdduser">添加司机</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table style="margin-top: 10px;text-align: center" border stripe :data="Tuser">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="姓名" prop="username" width="150px"></el-table-column>
        <el-table-column label="性别" prop="sex" width="80px"></el-table-column>
        <el-table-column label="所属单位" prop="unit"></el-table-column>
        <el-table-column label="电话" prop="mobile"></el-table-column>
        <el-table-column label="紧急联系电话" prop="mobiletwo"></el-table-column>
        <el-table-column label="角色" prop="role_name"></el-table-column>
        <el-table-column label="注册时间" prop="time">
          <template slot-scope="scope">{{ scope.row.time | dataForm }}</template>
        </el-table-column>
        <el-table-column label="状态" prop="operate" width="150px"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showuser(scope.row.id)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini"
                       @click="removeById(scope.row.user_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[1, 2, 5, 10]"
          :current-page="queryInfo.pagenum"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total" style="margin-top: 10px;">
      </el-pagination>
    </el-card>
    <!--  修改用户的对话框  -->
    <el-dialog
        title="修改用户信息"
        :visible.sync="showUserVisible"
        @close="editUserVisible"
        width="50%"
        top="10%">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
        <el-form-item label="用户名">
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="所属单位" prop="unit">
          <el-input v-model="editForm.unit"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="editForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="紧急联系电话" prop="mobiletwo">
          <el-input v-model="editForm.mobiletwo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="showUserVisible = false">取 消</el-button>
    <el-button type="primary" @close="showUserInfo">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    }
    return {
      // 获取用户列表的参数对象
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 1
      },
      //用户列表
      userlist: [],
      //数据条数
      total: 0,
      Tuser: [{
        username: '熊猫大侠',
        sex: '男',
        unit: '九江职业技术学院',
        mobile: '110',
        mobiletwo: '119',
        role_name: '开发员',
        time: '2021-5-1',
        operate: '已激活'
      }, {
        username: '张伟',
        sex: '男',
        unit: '爱情公寓',
        mobile: '123456789',
        mobiletwo: '110',
        role_name: '驾驶员',
        time: '2021-5-1',
        operate: '已激活'
      }, {
        username: '曾小贤',
        sex: '男',
        unit: '爱情公寓',
        mobile: '123456789',
        mobiletwo: '110',
        role_name: '驾驶员',
        time: '2021-5-1',
        operate: '已激活'
      }, {
        username: '吕子乔',
        sex: '男',
        unit: '爱情公寓',
        mobile: '123456789',
        mobiletwo: '110',
        role_name: '驾驶员',
        time: '2021-5-1',
        operate: '已激活'
      }
      ],
      //控制修改用户对话框的显示与隐藏
      showUserVisible: false,
      // 查询到的用户信息对象
      editForm: {},
      // 修改表单的验证规则对象
      editFormRules: {
        unit: [
          {required: true, message: '请输入居住地址', trigger: 'blur'},
        ],
        mobile: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {validator: checkMobile, trigger: 'blur'}
        ],
        mobiletwo: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {validator: checkMobile, trigger: 'blur'}
        ]
      },
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    //根据分页获取对应的司机列表
    async getUserList() {
      const {data: res} = await this.$http.get('registration', {params: this.queryInfo})
      if (res.meta.status !== 200) {
        return this.$message.error('获取司机列表失败!')
      }
      this.$message.success('获取司机列表成功!')
      this.userlist = res.data.users
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getUserList()
    },
    //根据id删除用户
    async removeById() {
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.error('已取消删除！')
      }
      const {data: res} = await this.$http.delete(`user/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败！')
      }
      this.$message.success('删除成功！')
      this.getUserList()
    },
    //编辑用户的对话框
    async showuser(id) {
      const {data: res} = await this.$http.get('users/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('查询用户信息失败！')
      }
      this.editForm = res.data
      this.showUserVisible = true
    },
    // 监听修改用户对话框的关闭事件
    editUserVisible() {
      this.$refs.editFormRef.resetFields()
    },
    //修改用户信息并提交
    showUserInfo() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        // 发起修改用户信息的数据请求
        const {data: res} = await this.$http.put(
            'users/' + this.editForm.id,
            {
              unit: this.editForm.unit,
              mobile: this.editForm.mobile,
              mobiletwo: this.editForm.mobiletwo
            }
        )

        if (res.meta.status !== 200) {
          return this.$message.error('更新用户信息失败！')
        }

        // 关闭对话框
        this.showUserVisible = false
        // 刷新数据列表
        this.getUserList()
        // 提示修改成功
        this.$message.success('更新用户信息成功！')
      })
    },
    //添加用户的对话框
    goAdduser() {
      this.$router.push('/driver/add')
    }
  }
}
</script>

<style Lang="less" scoped>
.el-table-column {
  background-color: #00b7ee;
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #08C0B9;
  color: #FFF;
}
</style>
